#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 2000000;
int tree[N];
int n, m, k;

struct Data
{
	int x, y;
} da[N];

bool compare(Data x, Data y)
{
	if (x.y < y.y) return true;
	if (x.y == y.y && x.x < y.x) return true;
	return false;
}

void update(int x, int d)
{
	while (x <= n) {
		tree[x] += d;
		x += x & (-x);
	}
}

int getsum(int x)
{
	int sum = 0;
	while (x > 0) {
		sum += tree[x];
		x -= (x & (-x));
	}
	return sum;
}

int sum(int l, int r)
{
	if (l > r) return 0;
	int L = getsum(l-1);
	int R = getsum(r);
	return R - L;
}

int main()
{
	int T;
	scanf("%d", &T);
	for (int I = 1; I <= T; ++I) {
		scanf("%d%d%d", &n, &m, &k);
		fill(tree, tree+N, 0);
		for (int i = 0; i != k; ++i)
			scanf("%d%d", &da[i].x, &da[i].y);
		sort(da, da+k, compare);

		long long ans = 0;
		for (int i = 0; i != k; ++i) {
			ans += sum(da[i].x + 1, n);
			update(da[i].x, 1);
		}
		printf("Test case %d: %lld\n", I, ans);
	}
}

